import 'bootstrap/dist/css/bootstrap.css';

import '../css/index2.scss';

require('bootstrap/dist/js/bootstrap.js');
require('./common.js');


$('#sidebar-menu a[href="website_board.html"]').addClass('active')
$('#sidebar-menu a[href="website_board.html"]').parent('li').addClass('active')

 require('./echartsFunction/chartClass.js');


 require('./echartsFunction/tool.js');





import { websiteComparisonUrl, websiteComparisonDetailUrl} from './urlconfig/config.js';

import {tableInit} from './tableFunction/cmmon.js';

import {chartData} from './lineConfig.js'; //获取多站的线条配置

import {getData, chartError, removeChartError} from './chartFunction.js';

let type = tool.getQueryString('type');
let datetime = tool.getQueryString('date');
let web_name = tool.getQueryString('web_name');




var resDataObj = {
	timeData: [],
	seriesData: {}
}, chart, table;

var reqOption = {
	data: {
			time: 30,
        	website_name: web_name,
        	website_time: datetime,
        	type: type
		},
	url: websiteComparisonUrl,
	type: 'post'		
}

function setTitle() {
	let type_str;
	if( type === 'drop') {
		type_str = '跌入第二页'
	}
	if(type === 'rise') {
		type_str = '涨入第一页'
	}
	$(".page-title").html(web_name+ type_str + '关键词对比其它站点变化');
	if(datetime.length === 0) {
		let newdate = new Date().toLocaleDateString();
		$(".jieshi span").text('当前对比日期为:' + newdate);
	}else {
		$(".jieshi span").text('当前对比日期为:' + datetime);
	}
	
}
setTitle();

function creat() {
	getData(reqOption).then(function(res){
		console.log(res)
		if(res.hasOwnProperty('items') || res.items.length > 0) {
			removeChartError($("#chart-content"));
			formatResData(res.items);
			chart = new ChartClass($("#chart-content")[0], chartData);
			chart.init();
			//设置站点的选项
           	res.items.forEach(function(v){
           		let option = $("<option></option>");
           		option.text(v.name).attr('data-id', v.id).val(v.name).appendTo($("#select-website"))
        		
           	})
                       
		}else {
			removeChartError($("#chart-content"));
			chartError($("#chart-content"))
		}
		
	}, function() {
		removeChartError($("#chart-content"));
		chartError($("#chart-content"))
	})
} 

function formatResData(resArr) {
	resDataObj.seriesData = {};
	resDataObj.timeData = [];
	let length = resArr.length;
	let webName = [];
	let xAxisTime = [];
	
	for(let index = 0; index < length; index++) {
		webName[index] = resArr[index].name;
		let obj = {};
		let i = 0, j = 0;
		let seriesData = [];
		let dataValue = resArr[index].data;
		for(var p in dataValue) {
			xAxisTime[j++] = tool.formatDate(parseInt(p));
			seriesData[i++] = dataValue[p];
			resDataObj.seriesData[resArr[index].name] = seriesData;
			// obj[resArr[index].name] = seriesData;
		}
		

	}
	resDataObj.timeData = xAxisTime;
	chartData.name = webName;
	chartData.data = resDataObj;
}		


creat();
// getData(reqOption, formatResData, true);



//   选中站点后查看关键词

let flag = true;

$('#select-website').on('change', function() {
	let option = $('#select-website  option:selected');
	let conparison_web = option.val();	
	
	let reqObj = {
		time: datetime,
		source_webname: web_name,
		other_webname: conparison_web,
    	type: type
	};
	if(flag) {
		operate(conparison_web);
		flag = false;
		$("#select-website option").eq(0).remove();
	}else {

		$('.fixed-table-loading').show();
		$.ajax({
			url: websiteComparisonDetailUrl,
			type: 'post',
			dataType: 'json',
			data: reqObj,
			success: function(res) {
				if(res.status === 1) {
					$('.fixed-table-loading').hide();
					table.$table.bootstrapTable('load', res.data.items); 
				}
			}
		})
	}
	

	// console.log($("#datatable-editable").height())
})

// 时间选择
function setTime(dagnumber, ele) {
	reqOption.data.time = dagnumber;
	let ele_siblins = tool.sibling(ele);
	for(let index = 0, length = ele_siblins.length; index < length; index++) {
		tool.removeClass(ele_siblins[index], 'btn-default');
	}
	tool.addClass(ele, 'btn-default');
	chartData.day = dagnumber;
	creat()
	
}
window.setTime = setTime;
function operate(conparison_web) {
	
	let reqObj = {
		time: datetime,
		source_webname: web_name,
		other_webname: conparison_web,
    	type: type
	};
	let columns = [
			{
                field: 'key_name',
                title: '关键词'
            },{
                field: 'old_rank',
                title: '原排名',
            },{
            	field: 'new_rank',
                title: '新排名',
            },{
                field: 'all_expoment',
                title: '整体指数'
            },{
                field: 'pc_expoment',
                title: 'pc指数',
                
            },{
                field: 'mobile_expoment',
                title: '移动指数',
            },{
            	field: 'name360_expoment',
            	title: '360指数',
            	
            }
		];
	let baseConfig = {
		list: {
			url: websiteComparisonDetailUrl,
			type: 'post',
			data: reqObj
		},
		table: {
			id: '#datatable-editable',
			columns: columns	
		},	
		//分页默认是bootstrap																	
		page: false, //是否需要分页
	};
	table = new tableInit(baseConfig);

}

